<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="heaerTopContent">
        <div class="grid-content bg-purple">
          <!-- <el-input v-model.trim="listQuery.createUserName" placeholder="输入用户名" style="width: 350px;" class="filter-item" @keyup.enter.native="handleFilter" /> -->
          <el-select v-model="listQuery.userId" clearable placeholder="请选择用户">
            <el-option
              v-for="item in optionsUser"
              :key="item.userId"
              :label="item.realName"
              :value="item.userId"
            />
          </el-select>
        </div>

        <div class="grid-content bg-purple">
          <el-button class="filter-item" type="primary" style="margin-left: 10px;" icon="el-icon-search" @click="handleFilter">
            搜索
          </el-button>
        </div>

        <div class="grid-content bg-purple">
          <el-button style="margin-left: 10px;" type="success" icon="el-icon-download" @click="downloadExcel">Excel入库模板下载</el-button>
        </div>
        <div class="grid-content bg-purple-light">
          <el-upload
            ref="upload"
            class="uploadExcel"
            style="margin-left:10px"
            action=""
            accept=".xlsx"
            :limit="limitNum"
            :auto-upload="false"
            :show-file-list="false"
            :on-change="uploadChange"
          >
            <el-button type="danger" icon="el-icon-document">入库</el-button>
          </el-upload>
        </div>
      </div>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      element-loading-text="拼命加载中"
      :data="tableData"
      fit
      border
      :default-sort="{prop: 'stockInTime', order: 'descending'}"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="序号" type="index" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ (listQuery.pageNo-1)*listQuery.pageSize+scope.$index +1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库时间" prop="stockInTime" sortable width="180" align="center">
        <template slot-scope="{row}">
          <span>{{ row.stockInTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库卡来源" align="center">
        <template slot-scope="{row}">
          <span>{{ row.cardSource }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库ID" align="center">
        <template slot-scope="{row}">
          <span>{{ row.stockInId }}</span>
        </template>
      </el-table-column>

      <el-table-column label="入库数量" prop="stockInAmount" class-name="status-col" width="200" align="center">
        <template slot-scope="{row}">
          {{ row.stockInAmount }}
        </template>
      </el-table-column>
      <el-table-column label="操作人" class-name="status-col" width="180" align="center">
        <template slot-scope="{row}">
          {{ row.createUserName }}
        </template>
      </el-table-column>
      <el-table-column label="添加备注" prop="remark" width="300" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div class="remarkBox">
            <el-input
              v-if="!scope.row.is_edit"
              v-model.lazy="scope.row.remark"

              size="small"
              placeholder="请输入备注信息"
              style="width:200px"
              @keyup.enter.native="inputRemark(scope.row)"
            />
            <a v-else href="javascript:;" class="inputStyle" :title="scope.row.remark"><el-tag size="small" type="danger">{{ scope.row.remark }}</el-tag></a>
            <div v-if="is_edit" style="margin-left: 15px;">
              <!-- 编辑 -->
              <el-button
                v-if="scope.row.is_edit"
                size="mini"
                type="danger"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
              >修改备注</el-button>
              <!-- 保存 -->
              <el-button
                v-if="!scope.row.is_edit"
                size="mini"
                type="success"
                icon="el-icon-plus"
                @click="handleSave(scope.$index, scope.row)"
              >保存备注</el-button>
            </div>
          </div>

        </template>
      </el-table-column></el-table>
    <!-- 页码 -->
    <div class="pageGation">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getputstorageList" />
    </div>
  </div>
</template>

<script>
// import parseTime from '@/utils/index'
import { getoutStorageList, putStorage, getUserId, editRemark } from '@/api/putStorage'
import Pagination from '@/components/Pagination'

import axios from 'axios'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      is_edit: true,
      remarkInfo: '',
      titleName: '入库',
      tableData: [],
      currentRow: null,
      tableKey: 0,
      fileList: [],
      limitNum: 1, // 上传个数
      total: 0,
      optionsUser: null,
      listQuery: {
        createUserName: '',
        userId: '',
        pageNo: 1,
        pageSize: 20
      },
      listLoading: false,
      newData: ''
    }
  },
  created() {
    this.getputstorageList()
    this.getUserIdSelect()
  },
  methods: {
    // 修改备注
    handleEdit(index, row) {
      console.log(index, row)
      if (row.remark) {
        row.is_edit = false
      }
    },
    inputRemark(item) {
      this.saveRemark(item)
    },
    // 保存数据
    handleSave(index, row) {
      console.log(row)
      this.saveRemark(row)
    },
    saveRemark(row) {
      editRemark({
        stockInId: row.stockInId,
        remark: row.remark
      }).then(res => {
        if (res.code === '00000') {
          row.is_edit = true
          this.$message.success(`${res.message}`)
          setTimeout(() => {
            this.getputstorageList()
          }, 200)
        }
      })
    },
    // 获取入库列表
    getputstorageList() {
      this.listLoading = true
      getoutStorageList(this.listQuery).then(res => {
        if (res.code === '00000') {
          this.listLoading = false
          this.total = res.data.totalRows
          this.tableData = res.data.rows.map(item => ({
            cardSource: item.cardSource,
            createUserName: item.createUserName,
            remark: item.remark,
            stockInAmount: item.stockInAmount,
            stockInId: item.stockInId,
            stockInTime: item.stockInTime,
            is_edit: !!item.remark
          }))
          // this.tableData = res.data.rows
        } else {
          this.$message.error('请求失败')
          this.listLoading = false
        }
      })
    },
    // 卡模板下载
    downloadExcel() {
      axios.get('static/cardExcel.xlsx', {
        responseType: 'blob'
      }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]))
        const link = document.createElement('a')
        const fname = '鼎音入库卡模板.xlsx'
        link.href = url
        link.setAttribute('download', fname)
        document.body.appendChild(link)
        link.click()
      })
    },
    // 获取用户下拉id
    async getUserIdSelect() {
      await getUserId().then(res => {
        if (res.code === '00000') {
          this.optionsUser = res.data
        }
      })
    },
    // 搜索
    handleFilter() {
      this.loading = true
      setTimeout(() => {
        this.loading = false
        this.getputstorageList()
      }, 200)
    },
    // 入库
    uploadChange(item) {
      const formData = new FormData()
      const file = item.raw
      formData.append('file', file)
      // 请求
      this.listLoading = true
      putStorage(formData).then(res => {
        if (res.code === '00000') {
          this.$message({
            message: '入库成功',
            type: 'success'
          })
          this.listLoading = false
          this.listQuery.userId = ''
          this.getputstorageList()
        } else {
          this.$message.error('入库失败')
          this.listLoading = false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.inputStyle{
width:170px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.remarkBox{
  display: flex;
  overflow: hidden;
  justify-content: center;
  align-items: center;
  padding-left: 25px;
  >a{
    display: inline-block;
    text-align: left;
    color: #f00;
  }
}
.el-table{
  position: relative;
    overflow: hidden;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    width: 100%;
    max-width: 100%;
    background-color: #fff;
    font-size: 14px;
    color: #606266;
}
.small-padding .cell {
    padding-left: 5px;
    padding-right: 5px;
}
.el-table .cell {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-break: break-all;
    line-height: 23px;
    padding-left: 10px;
    padding-right: 10px;
}
.heaerTopContent{
  height: 100%;
  clear: both;
  .grid-content{
    float: left;
  }
}
.pageGation{
  display: flex;
  justify-content: flex-end;
  margin: 15px 40px 0;
  font-size: 23px;
}

</style>
